package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.sgb.link.entity.merchant.InvestmentProjectEntity;
import com.sgb.link.entity.merchant.TradeCategoryEntity;
import com.sgb.merchant.api.MerchantsNet;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/6/17
 * Author:黄淋
 * Description:
 */
public class MerchantHomeViewmodel extends BaseViewModel {
    public MutableLiveData<List<TradeCategoryEntity>> liveDataCategoryList = new MutableLiveData<>();
    public MutableLiveData<ListResponse<List<InvestmentProjectEntity>>> liveDataInvestment = new MutableLiveData<>();
    public int pageNum = 1;
    public MutableLiveData<Integer> ldState=new MutableLiveData<>();
    public MerchantHomeViewmodel(@NonNull Application application) {
        super(application);
    }

    public void getInvestmentData(boolean isLoadMore, Map<String, Object> map) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("page", pageNum);
        map.put("limit", 20);
        MerchantsNet.getInstance().getService().getInvestmentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>> call, Response<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null || response.body().getData().getList() == null) {
                    liveDataInvestment.postValue(new ListResponse<List<InvestmentProjectEntity>>());
                    if(response.body() != null && !TextUtils.isEmpty(response.body().getMsg())) {
                        MToast.showToast(getApplication(), response.body().getMsg());
                    }
                    return;
                }
                if(response.body().isSuccess()) {
                    liveDataInvestment.postValue(response.body().getData());
                }else{
                    if(!TextUtils.isEmpty(response.body().getMsg())) {
                        MToast.showToast(getApplication(), response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<InvestmentProjectEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();

            }
        });

    }
    public void getState(Long no){
        MerchantsNet.getInstance().getService().getProjectState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if(data.isSuccess()) {
                    ldState.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 行业一级分类列表
     */
    public void getIndustryFirstCategoryList() {
        MerchantsNet.getInstance().getService().getindustryFirstCategoryList().enqueue(new ICallBack<BaseEntity<List<TradeCategoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TradeCategoryEntity>> data) {
                if(data.isSuccess()) {
                    liveDataCategoryList.postValue(data.getData());
                }
            }
        });

    }
}
